<?php

namespace App\Http\Controllers\Admin;

use App\Cate;
use App\Http\Controllers\Api\Controller;
use App\ImageIcon;
use App\Problem;
use App\Ad;
use App\User;
use App\Image;
use App\SubjectOne;
use App\SubOneIamgeCate;
use Illuminate\Http\Request;


class SuboneController extends Base
{

    /**
     * @param Request $request
     * @return mixed 400 题目重复 200添加成功
     */
    public function add(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $map['answer'] = implode(',',$map['answer']);
            $map['cate_id'] = implode(',',$map['cate_id']);
            $model = SubjectOne::add($map);
            return $model;
        }
    }


    /**
     * @param Request $request
     * @return mixed
     * 科目一列表
     */
    public function getlist(Request $request)
    {
        //获取传过来的页码
        $page  = $request->input('page');
        $list  = SubjectOne::with('image')
            ->with('chapters')
            ->offset(($page-1)*30)//每页30条数据
            ->limit(30)
            ->get()
            ->toArray();
        //如果没有图片返回空
        foreach ($list as $k=>$v){
            if($v['url']==''){
                $list[$k]['image']['url']='';
            }
        }
        $cate_id = '';
        //获取多个分类
        foreach ($list as $k=>$v){
            foreach (explode(',',$v['cate_id']) as $ki=>$vi){
                $list[$k]['cate'][] = Cate::where('id',$vi)->first();
            }
        }

        foreach ($list as $k=>$v){
            //判断是否为争议和易错题
            $list[$k]['is_zy']==1?$list[$k]['is_zy']=true:$list[$k]['is_zy']=false;
            $list[$k]['is_yct']==1?$list[$k]['is_yct']=true:$list[$k]['is_yct']=false;

        }
        //总数据
        $zpage = SubjectOne::count();
        //返回当前页码数据和总数
        return $this->response->array([
            'data'=>$list,
            'total'=>$zpage,
            'session'=>session('adminuser'),
        ]);
    }

    /**
     * @param Request $request
     * @return mixed
     * 科目一列表
     */
    public function SubOneSearch(Request $request)
    {
        //获取传过来的页码
//        $page  = $request->input('page');
        if($request->input('chapters_id')==''){
            $list  = SubjectOne::with('image')
                ->with('chapters')
                ->where('question','like','%'.$request->input('title').'%')
                ->get()
                ->toArray();
        }else{
            $list  = SubjectOne::with('image')
                ->with('chapters')
                ->where('question','like','%'.$request->input('title').'%')
                ->where('chapters_id',$request->input('chapters_id'))
                ->get()
                ->toArray();
        }


        //如果没有图片返回空
        foreach ($list as $k=>$v){
            if($v['url']==''){
                $list[$k]['image']['url']='';
            }
        }
        $cate_id = '';
        //获取多个分类
        foreach ($list as $k=>$v){
            foreach (explode(',',$v['cate_id']) as $ki=>$vi){
                $list[$k]['cate'][] = Cate::where('id',$vi)->first();
            }
        }

        foreach ($list as $k=>$v){
            //判断是否为争议和易错题
            $list[$k]['is_zy']==1?$list[$k]['is_zy']=true:$list[$k]['is_zy']=false;
            $list[$k]['is_yct']==1?$list[$k]['is_yct']=true:$list[$k]['is_yct']=false;

        }
        if($request->input('chapters_id')==''){
            //总数据
            $zpage = SubjectOne::
            where('question','like','%'.$request->input('title').'%')
                ->count();
        }else{
            //总数据
            $zpage = SubjectOne::
            where('question','like','%'.$request->input('title').'%')
                ->where('chapters_id',$request->input('chapters_id'))
                ->count();
        }

        //返回当前页码数据和总数
        return $this->response->array([
            'data'=>$list,
            'total'=>$zpage,
            'session'=>session('adminuser'),
        ]);
    }


    /**
     * 科目一题目删除
     * @param Request $request
     */
    public function delsid(Request $request)
    {
        if($request->isMethod('post')){
            $id = $request->input('id');
            SubjectOne::where('id',$id)->delete();
        }
    }


    public function serachimage(Request $request)
    {
        $list = ImageIcon::with('image')->with('level1')
            ->with('level2')->where('name','like','%'.$request->p.'%')->limit(10)->get();
        return $this->response->array([
            'data'=>$list
        ]);
    }


    /**
     * @param $id
     * @return mixed
     * 获取修改参数
     */
    public function save(Request $request)
    {
        $id = $request->input('id');
        $list = SubjectOne::with('image')->where('id',$id)->first();
        $list['answer'] = explode(',',$list['answer']);
        $list['cate_id'] = explode(',',$list['cate_id']);
        foreach ($list['cate_id'] as $v){
            $cate_id[] = intval($v);
        }
        $list['cate_id'] = $cate_id;
        if($list['url']!=''){
            $image['name'] = $list['question'];
            $image['url'] = $list['image']['url'];
            $images[] = $image;
        }else{
            $images = '';
        }

        return $this->response->array([
            'data'=> $list,
            'image'=>$images
        ]);
    }

    /**
     * 修改科一题目
     * @return int
     */
    public function saveone(Request $request)
    {
        if($request->isMethod('post')){
            $map = $request->input();
            unset($map['image']);
            $map['answer'] = implode(',',$map['answer']);
            $map['cate_id'] = implode(',',$map['cate_id']);
            $rwe = SubjectOne::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    /**
     * 修改争议题状态
     * @param Request $request
     * @return mixed
     */
    public function zy(Request $request)
    {
        if($request->isMethod('post')){
            $map = $request->input();
            $rwe = SubjectOne::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    /**
     * 修改易错题状态
     * @param Request $request
     * @return mixed
     */
    public function yc(Request $request)
    {
        if($request->isMethod('post')){
            $map = $request->input();
            $rwe = SubjectOne::where('id',$map['id'])->update($map);
            return $rwe;
        }
    }


    /**
     * 图片分类添加
     * @param Request $request
     * @return mixed
     */
    public function cateadd(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $model = SubOneIamgeCate::add($map);
            return $model;
        }
    }


    /**
     * 科目一图片分类
     * @return mixed
     */
    public function GetSubOneIamgeCate()
    {
        $model = SubOneIamgeCate::SubOneIamgeCate();
        return $this->response->array($model);
    }

    public function GetImageCate()
    {
        $model = SubOneIamgeCate::get();
        $zpage = SubOneIamgeCate::count();

        return $this->response->array([
            'data'=>$model,
            'total'=>$zpage,
        ]);
    }


    /**
     * 图片分类2
     * @param Request $request
     * @return array|null|string
     */
    public function GetImageLevel2(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input('id');
            $model = SubOneIamgeCate::where('pid',$map)->get();
            return $this->response()->array($model);
        }
    }


    /**
     * 图标图片上传
     * @param Request $request
     * @return mixed
     */
    public function imageiconadd(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            $rew = ImageIcon::add($map);
            return $rew;
        }
    }


    /**
     * 图标列表
     * @param Request $request
     * @return mixed
     */
    public function GetImageList(Request $request)
    {

        $page  = $request->input('page');
        $list  = ImageIcon::with('image')
            ->with('level1')
            ->with('level2')
            ->offset(($page-1)*100)
            ->limit(100)
            ->get()
            ->toArray();


        foreach ($list as $k=>$v){
            if($v['level2']==''){
                $list[$k]['level2']['name']='';
            }
        }


        $zpage = ImageIcon::count();

        return $this->response->array([
            'data'=>$list,
            'total'=>$zpage,
        ]);
    }


    /**
     * 删除图片
     * @param Request $request
     * @return mixed
     */
    public function DelImageList(Request $request)
    {
        if($request->isMethod('post')){
           $rew = ImageIcon::where('id',$request->input('id'))->delete();
           return $rew;
        }
    }


    /**
     * @param Request $request
     * @return mixed
     * 获取图标信息
     */
    public function GetImafeInfo(Request $request)
    {
        if ($request->isMethod('post')){
            $list = ImageIcon::
                where('id',$request->input('id'))
                ->with('image')
                ->first();

            if($list['image']!=''){
                $image['name'] = $list['name'];
                $image['url'] = $list['image']['url'];
                $images[] = $image;
            }else{
                $images = '';
            }
            return $this->response->array([
                'data'=>$list,
                'image'=>$images
            ]);
        }
    }


    public function imageiconsave(Request $request)
    {
        if ($request->isMethod('post')){
            $map = $request->input();
            unset($map['image']);
            $rew = ImageIcon::where('id',$map['id'])->update($map);
            if($rew){
                return 200;
            }else{
                return 400;
            }
        }
    }


    /**
     * 科目一常见问题
     * @param Request $request
     * @return int
     */
    public function ProblemAdd(Request $request)
    {
        if($request->isMethod('post')){
            $model = Problem::add($request->input());
            return $model;
        }
    }

    /**
     * 科目一常见问题列表
     * @param Request $request
     * @return int
     */
    public function getproblemlist(Request $request)
    {
        if($request->isMethod('post')){
            $list = Problem::get();
            return $this->response->array([
                'data'=>$list
            ]);
        }
    }


    /**
     * 常见问题详情
     * @param Request $request
     * @return int
     */
    public function getprobleminfo(Request $request)
    {
        if($request->isMethod('post')){
            $list = Problem::where('id',$request->input('id'))->first();
            return $this->response->array([
                'data'=>$list
            ]);
        }
    }

    /**
     * 常见问题详情修改
     * @param Request $request
     * @return int
     */
    public function Problemsave(Request $request)
    {
        if($request->isMethod('post')){
            $model = Problem::saves($request->input());
            return $model;
        }
    }
    /**
     * 常见问题详情删除
     * @param Request $request
     * @return int
     */
    public function problemdel(Request $request)
    {
        if($request->isMethod('post')){
            $model = Problem::where('id',$request->input('id'))->delete();
            return $model;
        }
    }


    public function adadd(Request $request)
    {
        $rew = Ad::create($request->input());

        return 200;
    }

    public function adlist(Request $request)
    {
        $list  = Ad::orderBy('updated_at','desc')->get();
        $list = $list->map(function ($v){
            return [
                'http'=>$v['http'],
                'url'=>(Image::where('id',$v['url'])->first())['url'],
                'type'=>$v['type'],
                'id'=>$v['id']
            ];
        });
        return $this->response->array([
            'data'=>$list
        ]);
    }


    public function delad(Request $request)
    {
        Ad::where('http',$request->http)->delete();
        return 200;
    }



    public function adinfo(Request $request)
    {
        $data = Ad::where('id',$request->id)->first();

        return $this->response->array([
            'data'=>$data
        ]);
    }



    public function adsave(Request $request)
    {
        $data = Ad::where('id',$request->id)->update($request->input());

        return 200;
    }


    public function getuserlist(Request $request)
    {
        $list = User::orderBy('created_at','desc')->get();

        return $this->response->array([
            'data'=>$list
        ]);
    }



}
